<template>
  <div class="publish-container">
    <el-card>
      <div slot="header">
        <my-breadcrumb>发布文章</my-breadcrumb>
      </div>
      <el-form ref="form" :rules="rules" :model="article" label-width="80px">
  <el-form-item label="标题" prop="title">
    <el-input v-model.trim="article.title"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <!-- <el-input type="textarea" v-model="article.content"></el-input> -->
    <quill-editor v-model="article.content" :options="editorOption"></quill-editor>
  </el-form-item>
   <el-form-item label="封面">
     <!-- @change="article.cover.images=[]"切换封面类型的时候重置数据 -->
    <el-radio-group v-model="article.cover.type" @change="article.cover.images=[]">
      <!-- 封面类型：根据后端接口约定 -->
      <el-radio :label="1">单图</el-radio>
      <el-radio :label="3">三图</el-radio>
      <el-radio :label="0">无图</el-radio>
      <el-radio :label="-1">自动</el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item>
    <el-row :gutter="10" v-if="article.cover.type > 0">
      <el-col v-for="(item, idx) in article.cover.type" :key="idx" :xs="8" :sm="6" :md="6" :lg="4">
        <my-cover v-model="article.cover.images[idx]"></my-cover>
      </el-col>
    </el-row>
  </el-form-item>
  <!-- <el-form-item label="频道" prop="channel_id">
    <el-select v-model="article.channel_id" placeholder="请选择频道">
      <el-option
      v-for="channel in channels"
      :key="channel.id"
      :label="channel.name"
      :value="channel.id"></el-option>
    </el-select>
  </el-form-item> -->
  <my-channels v-model="article.channel_id"></my-channels>
  <el-form-item>
    <el-button type="primary" @click="hAddArticle(false)">发布</el-button>
    <el-button @click="hAddArticle(true)">存入草稿</el-button>
  </el-form-item>
</el-form>
    </el-card>
  </div>
</template>

<script>
import MyCover from '@/components/MyCover.vue';
import MyChannels from '@/components/MyChannelsVmodel.vue';
import MyBreadcrumb from '../../components/MyBreadcrumb.vue';
import { addArticle } from '@/api/article';
// 导入样式
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
// 导入组件
import { quillEditor } from 'vue-quill-editor';
export default {
  name: 'AddArticle',
  props: { },
  data () {
    return {
      // channels: [], // 频道列表
      article: {
        title: '', // 文章标题
        content: '', // 文章内容
        cover: {
          type: 0, // 封面类型，图片张数
          images: [] // 封面的地址
        },
        channel_id: '' // 频道id
      },
      editorOption: { // quillEditor富文本编辑器的配置项
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      // 表单验证
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5到 30个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字符', trigger: 'blur' }
        ],
        channel_id: [
          { required: true, message: '请选择文章频道', trigger: 'blur' }
        ]
      }
    };
  },
  components: {
    MyBreadcrumb,
    quillEditor,
    MyChannels,
    MyCover
  },
  computed: { },
  created () {
    // 获取并显示频道
    // this.loadChannels();
  },
  mounted () { },
  methods: {
    // async loadChannels () {
    //   try {
    //     const res = await getArticleChannels();
    //     this.channels = res.data.data.channels;
    //   } catch (error) {
    //     this.$message.error(error);
    //   }
    // },
    // 发布文章
    // 表单验证:
    // 1.建立验证规则
    // 2.给表单项绑定rules和prop
    // 3.在提交之前，调用form组件的验证方法validate
    hAddArticle (isDraft) {
      // 先做表单验证validate
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 验证通过
          try {
            await addArticle(isDraft, this.article);
            this.$message.success('发布成功');
            // 清空
            this.article.title = '';
            this.article.content = '';
          } catch (error) {
            console.log(error.message);
            this.$message.error('发布失败');
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped lang='less'>
// 覆盖富文本样式(publish-container目的提高选择器优先级)
// 加上/deep/实现穿透能力
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
